﻿using System;

namespace Hvam.SharePoint.Search.Language.Parsers
{
    internal class CharParser
    {
        public static Parser<char> Char(Predicate<char> predicate)
        {
            return 
                input =>
                    {
                        if (!input.AtEnd() && predicate(input.Current()))
                        {
                            return new Success<char>(input.Current(), input++);
                        }

                        return new Failure<char>(input);
                    };
        }

        public static Parser<char> Char(char @char)
        {
            return Char(c => c == @char);
        }

        public static readonly Parser<char> WhiteSpace = Char(char.IsWhiteSpace);
        public static readonly Parser<char> Digit = Char(char.IsDigit);
        public static readonly Parser<char> Letter = Char(char.IsLetter);

        public static Parser<T> End<T>()
        {
            return
                input =>
                    {
                        if (input.AtEnd())
                        {
                            return new Success<T>(default(T), input);
                        }
                        return new Failure<T>(input);
                    };
        }
    }
}